local Msg = require("core.Msg")

local Grid = require("bag.Grid")
local MailLogicFW = require("mail.MailLogicFW")
local MailDefine = require("mail.Define")
local Lang = require("common.Lang")
--local Util = require("common.Util")
local InnerDataDefine = require("innerdata.Define")
local InnerDataManagerFW = require("innerdata.InnerDataManagerFW")

function makeMail(mailNode, mail)
    mailNode.mailID = mail.mailID
    mailNode.title = mail.title and tostring(mail.title) or ""
    mailNode.content = mail.content or ""
    mailNode.sendTime = mail.sendTime
    mailNode.isRead = mail.isRead or 0

    local len = 0
    if mail.gridList then
        for k, v in pairs(mail.gridList) do
            len = len + 1
            Grid.makeItem(v, mailNode.items[len], 0)
            if len == 6 then
                break
            end
        end
    end
    mailNode.items[0] = len
end



function makeLeftMailNode(mailBase,mail)
    mailBase.mailID = mail.mailID
    mailBase.title = mail.title and tostring(mail.title) or ""
    mailBase.sendTime = mail.sendTime
    mailBase.isRead = mail.isRead or 0
    mailBase.hasItem = (mail.gridList and #mail.gridList > 0) and 1 or 0
end

-- 请求信件列表（左）
function CG_ASK_MAIL_LIST(human, always_send)
    local mails = MailLogicFW.GetMails(human)
    local gcRet = Msg.gc.GC_ASK_MAIL_LIST
    gcRet.mailList[0] = mails[0]
    for i = 1, gcRet.mailList[0] do
        local mail = mails[i]
        --local mailNode = gcRet.mailList[gcRet.mailList[0] - i + 1]
        local mailNode = gcRet.mailList[i]
        makeLeftMailNode(mailNode, mail, human.db.name)
    end
    if always_send or (0 < gcRet.mailList[0]) then
        Msg.send(gcRet, human.fd)
    end
end

function CG_FETCH_ATTACHMENT(human, msg)
    local mails = msg.mailIDList
    if mails[0] <= 0 or mails[0] > 100 then
        return
    end
    for i = 1, mails[0] do
        local mailid = mails[i]
        local nMailData = MailLogicFW.GetMailByID(human, mailid)

        if nMailData == nil then
            return Lang.MAIL_MAIL_ID_INVALID
        else
            if nMailData.receiverUuid ~= human._id then
                return Lang.MAIL_MAIL_IS_NOT_OWN_U
            end

            if nMailData.gridList == nil then
                return Lang.MAIL_NO_ATTACHMENT
            end

            local gcRet = Msg.gc.GC_MAIL_DETAIL
            makeMail(gcRet.mail, nMailData, human.name)
            InnerDataManagerFW.Send2LInnerData(human, InnerDataDefine.INNERDATA_TYPE_MAIL, gcRet)
        end
    end
end


function CG_READ_MAIL(human, msg)
    for i = 1, msg.mailID[0] do
        local mail = MailLogicFW.GetMailByID(human, msg.mailID[i])
        if mail and mail.receiverUuid == human._id then
            if MailDefine.MAIL_TYPE_SYSTEM == mail.mailType then
                MailLogicFW.SetMailRead(human, mail)
            else
                MailLogicFW.DelMail(mail)
            end
        end
    end
end

function CG_MAIL_DETAIL(human, msg)
    local mail = MailLogicFW.GetMailByID(human, msg.mailID)
    if mail and mail.receiverUuid == human._id then
        MailLogicFW.SetMailRead(human, mail)
        local gcRet = Msg.gc.GC_MAIL_DETAIL
        makeMail(gcRet.mail, mail, human.name)
        Msg.send(gcRet, human.fd)
    end
end


function CG_DEL_MAIL(human, msg)
    --[[
    local msgErr = Msg.gc.GC_BROADCAST
    msgErr.type = Lang.CHAT
    --]]
    local gcRet = Msg.gc.GC_DEL_MAIL
    local len = 0

    for i = 1, msg.mailIDList[0] do
        local mail = MailLogicFW.GetMailByID(human, msg.mailIDList[i])
        if mail and mail.receiverUuid == human._id  then
            MailLogicFW.DelMail(mail)
            len = len + 1
            gcRet.mailIDList[len] = msg.mailIDList[i]
        end
    end
    gcRet.mailIDList[0] = len
    Msg.send(gcRet,human.fd)
    --[[
    msgErr.msg = Lang.MAIL_DEL_OK
    Msg.send(msgErr,human.fd)
    --]]
end

function LW_SENDMAIL(null, msg)
    local nMailTitle = msg.title
    local nMailContent = msg.content
    local nMailItemList = msg.itemlist
    if not nMailTitle then
        return false
    end
    local grids = {}
    local nLen = nMailItemList[0]
    if nLen > 0 then
        for i = 1, nLen do
            local nItemData = nMailItemList[i]
            if nItemData then
                grids[#grids + 1] = Grid.create(nil, nItemData.id, nItemData.cnt, true)
            end
        end
    end
    MailLogicFW.addSystemMessage(msg.uuid, nMailTitle, nMailContent, grids)
end
